package com.example.husbandry_online.view;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.husbandry_online.Constants.UserConstants;
import com.example.husbandry_online.MainActivity;
import com.example.husbandry_online.R;
import com.example.husbandry_online.Util.RandomCode;
import com.example.husbandry_online.model.User;
import com.example.husbandry_online.retrofit.ResponseModel;
import com.example.husbandry_online.retrofit.ServiceGetter;
import com.example.husbandry_online.service.UserService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class RegisterFragment extends Fragment {
    View root;
    Button register;
    Button sendCode;
    EditText userName;
    EditText passWord;
    EditText telePhone;
    EditText Code;
    String username;
    String password;
    String telephone;
    String code;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if(root == null) {
            root = inflater.inflate(R.layout.fragment_register, container, false);
        }
        return root;
    }

    //注册按钮 点击事件
    public void registerButtonOnclick(){
        register = getActivity().findViewById(R.id.register);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //得到参数
                userName = (EditText)getActivity().findViewById(R.id.username);
                username = userName.getText().toString();
                passWord = (EditText)getActivity().findViewById(R.id.password);
                passWord.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                password = passWord.getText().toString();
                telePhone = (EditText)getActivity().findViewById(R.id.telephone);
                telephone = telePhone.getText().toString();
                Code = (EditText)getActivity().findViewById(R.id.code);
                code = Code.getText().toString();

                //Log.v("ljh","111"+username+password+telephone+code);
                UserService userService = ServiceGetter.getService(UserService.class);
                //Log.v("ljh",newUser.toString());
                userService.registry(new User("0",username,telephone,password, code)).enqueue(new Callback<ResponseModel<User>>() {
                    @Override
                    public void onResponse(Call<ResponseModel<User>> call, Response<ResponseModel<User>> response) {
                        String result = response.body().getResult();

                        if (!result.equals("")){
                            if(result.equals("SUCCESS")){
                                Toast.makeText(getContext(),"注册成功",Toast.LENGTH_LONG).show();
                                changeRegisterFrag(new LoginFragment());

                            }else {
                                //Log.v("ljh","message:"+response.body().getMessage());
                                Toast.makeText(getContext(),"注册失败",Toast.LENGTH_SHORT).show();
                                Toast.makeText(getContext(),"错误:"+response.body().getMessage(),Toast.LENGTH_LONG).show();
                            }
                        }else {
                            Toast.makeText(getContext(),"网络繁忙，请重新尝试",Toast.LENGTH_LONG).show();
                        }

                    }
                    @Override
                    public void onFailure(Call<ResponseModel<User>> call, Throwable t) {
                        //Log.v("ljh","失败了");
                        Toast.makeText(getContext(),"网络繁忙，请重新尝试",Toast.LENGTH_LONG).show();

                        t.printStackTrace();
                    }
                });
            }
        });
    }

    //发送验证码按钮 点击事件
    public void sendCodeButtonOnclick(){
        sendCode = getActivity().findViewById(R.id.sendCodeInRegister);
        sendCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                telePhone = getActivity().findViewById(R.id.telephone);
                telephone = telePhone.getText().toString();
                RandomCode.createAlertAndCode(getContext(),getActivity(),telephone,getResources().getColor(R.color.red),UserConstants.CURRENT_PAGE_IS_REGISTER);

//                UserService userService = ServiceGetter.getService(UserService.class);
//                userService.sendCode(telephone).enqueue(new Callback<ResponseModel<String>>() {
//                    @Override
//                    public void onResponse(Call<ResponseModel<String>> call, Response<ResponseModel<String>> response) {
//                        String result = response.body().getResult();
//                        //Toast.makeText(getContext(),"result:"+result,Toast.LENGTH_LONG).show();
//                        if (!result.equals("")){
//                            if(result.equals("SUCCESS")){
//                                Toast.makeText(getContext(),"验证码发送成功，请稍候",Toast.LENGTH_LONG).show();
//                            }else {
//                                Toast.makeText(getContext(),"验证码发送失败",Toast.LENGTH_LONG).show();
//                            }
//                        }else {
//                            Toast.makeText(getContext(),"网络繁忙，请重新尝试",Toast.LENGTH_LONG).show();
//                        }
//                    }
//                    @Override
//                    public void onFailure(Call<ResponseModel<String>> call, Throwable t) {
//                        t.printStackTrace();
//                    }
//                });
            }
        });
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //注册 按钮点击事件
        registerButtonOnclick();

        //发送验证码  按钮点击事件
        sendCodeButtonOnclick();
    }
    //LoginAndRegisterActivity
    public void changeRegisterFrag(Fragment newFragment){
        //Log.v("ljh","sadsadsadas");
        LoginAndRegisterActivity activity = (LoginAndRegisterActivity) getActivity();
        activity.changeFragment(newFragment);
    }

}